/**
 * 
 */
package com.muc.modle;

import java.util.List;
import com.muc.modle.Status;
import com.muc.util.ConstTag;

import android.R.integer;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter implements ConstTag
{
	public Context		context;
	public List<Status>	alls;
	private int			lastLayout	= 0;
	private int			nowLayout	= 0;
	private ViewHolder	holder;

	public MyAdapter(Context con, List<Status> list)
	{
		// TODO 自动生成的构造函数存根
		context = con;
		alls = list;
		lastLayout = list.get(0).getDefaultLayout();
	}
	@Override
	public int getCount()
	{
		// TODO 自动生成的方法存根
		return alls.size();
	}
	@Override
	public Object getItem(int position)
	{
		// TODO 自动生成的方法存根
		return alls.get(position);// 如果用户选中了中间项
	}
	@Override
	public long getItemId(int position)
	{
		// TODO 自动生成的方法存根
		return position;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		// TODO 自动生成的方法存根
		if (getCount() == 0)
		{
			return null;
		}
		System.out.println("now position is "+position);
		nowLayout = alls.get(position).getDefaultLayout();
		convertView = LayoutInflater.from(context).inflate(nowLayout, null);
		setHolder(position, convertView);
		setHolderContent(position);
		return convertView;
	}
	private void setHolder(int position, View v)
	{
		holder = new ViewHolder(alls.get(position).getItemCount());
		System.out.println(((int[]) alls.get(0).getItemCount())[0]+" "+((int[]) alls.get(0).getItemCount())[1]);
		int i = 0, j = 0;
		for (MyContent mContent : alls.get(position).getContentList())
		{
			switch (mContent.getStyle())
			{
				case STYLE_ImageView:
					System.out.println("before i=" + i);
					holder.ivItem[i] = (ImageView) v.findViewById(mContent.getContentlayout());
					i++;
					System.out.println("after i=" + i);
					break;
				case STYLE_TextView:
					System.out.println("before j=" + j);
					holder.tvItem[j] = (TextView) v.findViewById(mContent.getContentlayout());
					j++;
					System.out.println("after j=" + j);
					break;
			}
		}
		System.out.println("before i,j=" + i + "  " + j);
	}
	private void setHolderContent(int position)
	{
		int i = 0, j = 0;
		for (MyContent mContent : alls.get(position).getContentList())
		{
			switch (mContent.getStyle())
			{
				case STYLE_ImageView:
					holder.ivItem[i].setImageResource((Integer) mContent.getContent());
					i++;
					break;
				case STYLE_TextView:
					holder.tvItem[j].setText((String) mContent.getContent());
					j++;
					break;
			}
		}
	}

	static class ViewHolder
	{
		ImageView	ivItem[];	// 图片集合
		TextView	tvItem[];	// 文字集合

		public ViewHolder(int item[])
		{
			ivItem = new ImageView[item[0]];
			tvItem = new TextView[item[1]];
			// TODO Auto-generated constructor stub
		}
	}
}
